﻿using BCCommon;
using BCCommon.Enums;
using BCCommon.TMS.Order;
using BCCommon.TMS.PushMessage;
using BCData.Common.FlowRecord;
using BCData.Common.PushMessageConfig;
using BCData.TMS.Driver;
using BCData.TMS.Order.Order;
using BCData.TMS.Order.OrderExtention;
using BCData.TMS.Order.OrderPool;
using BCDto.TMS.Order;
using BCDto.TMS.Order.OrderExtention;
using BCEntity.Common.EntityNameConst;
using BCEntity.Common.PushMessageConfig;
using BCEntity.MongoDB.PushMessage;
using BCService.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.Common.PushMessage.PushMessageSend.Order
{
    public class PushMessageForOrderService : IPushMessageForOrderService
    {
        private readonly IRedisService redisService;
        private readonly IPushMessageConfigData pushMessageConfigData;
        private readonly IOrderPoolData orderPoolData;
        private readonly IOrderData orderData;
        private readonly IOrderExtentionData orderExtentionData;
        private readonly IDriverData driverData;
        private readonly IUserFlowRecordData userFlowRecordData;

        public PushMessageForOrderService(IOrderPoolData orderPoolData,
            IOrderData orderData,
            IOrderExtentionData orderExtentionData,
            IRedisService redisService,
            IPushMessageConfigData pushMessageConfigData,
            IDriverData driverData,
            IUserFlowRecordData userFlowRecordData)
        {
            this.orderPoolData = orderPoolData;
            this.orderData = orderData;
            this.orderExtentionData = orderExtentionData;
            this.redisService = redisService;
            this.pushMessageConfigData = pushMessageConfigData;
            this.driverData = driverData;
            this.userFlowRecordData = userFlowRecordData;
        }

        public PushMessageMongoEntity GetPushMessageMongoEntity(long orderId, PushMessageType pushMessageType)
        {
            PushMessageMongoEntity pushMessageMongoEntity = new PushMessageMongoEntity();

            PushMessageConfigEntity pushMessageConfigEntity = GetPushMessageConfigInfo(pushMessageType);

            OrderDto orderDto = this.orderData.GetEntity(orderId).GetAwaiter().GetResult().As<OrderDto>();

            switch (pushMessageType)
            {
                case PushMessageType.AssignedVehicle:
                    MakeAssignedVehicle(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                case PushMessageType.BoundVehicleDistince:
                    MakeBoundVehicle(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                //case PushMessageType.Acceptance:
                //    MakeAcceptance(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                //case PushMessageType.Emergency:
                //    MakeEmergency(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                case PushMessageType.UserCancel:
                    MakeUserCancel(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                case PushMessageType.DriverCancel:
                    MakeDriverCancel(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                case PushMessageType.Paid:
                    MakePaid(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                //case PushMessageType.UserIncome:
                //    MakeUserIncome(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                //case PushMessageType.DriverIncome:
                //    MakeDriverIncome(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                //case PushMessageType.DriverRegister:
                //    MakePartnerIncome(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                //case PushMessageType.OrderInfoFee:
                //    MakeOrderInfoFee(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                case PushMessageType.DriverCancelFailed:
                    MakeDriverCancelFailed(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                //case PushMessageType.DriverCancelPassed:
                //    MakeDriverCancelPassed(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                //    break;
                case PushMessageType.Complete:
                    MakeOrderComplete(orderDto, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
            }
            return pushMessageMongoEntity;
        }

        #region 获取推送配置信息
        private PushMessageConfigEntity GetPushMessageConfigInfo(PushMessageType pushMessageType)
        {
            string key = string.Format("{0}:PushMessageType={1}", EntityNameConst.PushMessageConfigEntity, pushMessageType.ToString());

            var result = this.redisService.TryGet(key, () =>
            {
                var data = this.pushMessageConfigData.GetEntity(pushMessageType).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromDays(1));
            return result;
        }
        #endregion

        #region 指派车辆时 
        private void MakeAssignedVehicle(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            var list = this.orderExtentionData.GetList(orderDto.OrderNo).GetAwaiter().GetResult().As<IEnumerable<OrderExtentionDto>>().ToList();
            orderDto.OrderExtentionInfo.AddRange(list);
            int minIndex = orderDto.OrderExtentionInfo.Min(p => p.EndIndex);
            int maxIndex = orderDto.OrderExtentionInfo.Max(p => p.EndIndex);
            OrderExtentionDto minOrderExtentionDto = orderDto.OrderExtentionInfo.Single(p => p.EndIndex == minIndex);
            OrderExtentionDto maxOrderExtentionDto = orderDto.OrderExtentionInfo.Single(p => p.EndIndex == maxIndex);

            var orderPoolEntity = this.orderPoolData.Get(orderDto.OrderId).GetAwaiter().GetResult();

            var driverEntity = this.driverData.GetDriverByIdAsync(orderPoolEntity.DriverId.Value).GetAwaiter().GetResult();

            //{0}, {1} 到 {2} 发货地距您：{3}公里，订单金额：{4} 元  （结果：零担，陕西省西安市新城区 到 北京市东城区 发货地距您：1.5公里，订单金额：11335 元）
            string body = pushMessageConfigEntity.MessageTemplate;
            string amount = orderDto.OrderAmount.Value.ToString().Replace(".00", "");
            body = string.Format(body, orderDto.TransportBusinessStr, minOrderExtentionDto.AreaName, maxOrderExtentionDto.AreaName, orderPoolEntity.Distance, amount);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.DriverApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { driverEntity.UserId };
        }
        #endregion

        #region 司机已接单 
        private void MakeBoundVehicle(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，您的订单已被 {0} 车主接单成功，请您做好装货准备哦！！！  （结果：亲，您的订单已被 陕A00001 车主接单成功，请您做好装货准备哦！！！）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.VehicleNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 订单已签收
        private void MakeAcceptance(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，您的订单 {0} 已被成功签收  （结果：亲，您的订单 ZFWLP1901022XXQXBB 已被成功签收）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 订单已完成 
        private void MakeComplete(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            List<long> userIdList = new List<long>();
            if (orderDto.PayMode == PayMode.PaidReceipt)
            {
                var driverEntity = this.driverData.GetDriverByIdAsync(orderDto.DriverId.Value).GetAwaiter().GetResult();
                userIdList.Add(driverEntity.UserId);
            }

            //订单完成后推送用户（回单付订单推送司机）
            //亲，您的订单 {0} 已完成  （结果：亲，您的订单 ZFWLP1901022XXQXBB 已完成）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = orderDto.PayMode == PayMode.PaidReceipt ? AppType.DriverApp : AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = orderDto.PayMode == PayMode.PaidReceipt ? userIdList.ToArray() : new long[] { orderDto.UserId };
        }
        #endregion

        #region 司机遇到紧急情况
        private void MakeEmergency(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，您的订单收到一条紧急情况信息，请尽快查看！！！订单编号：{0}  （结果：亲，您的订单收到一条紧急情况信息，请尽快查看！！！订单编号：ZFWLP1901022XXQXBB）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 用户取消订单 
        private void MakeUserCancel(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            var driverEntity = this.driverData.GetDriverByIdAsync(orderDto.DriverId.Value).GetAwaiter().GetResult();
            //抱歉，订单已被用户取消。订单编号：{0} （结果：抱歉，订单已被用户取消。订单编号：ZFWLP1901022XXQXBB）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.DriverApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { driverEntity.UserId };
        }

        #endregion

        #region 司机取消订单 
        private void MakeDriverCancel(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，您的订单已被司机取消，请您保持电话畅通，我们稍后会与您取得联系。  （结果：亲，您的订单已被司机取消，请您保持电话畅通，我们稍后会与您取得联系。）
            string body = pushMessageConfigEntity.MessageTemplate;

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 订单运费支付完成 
        private void MakePaid(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            var driverEntity = this.driverData.GetDriverByIdAsync(orderDto.DriverId.Value).GetAwaiter().GetResult();
            //亲，货主已完成订单运费支付。订单编号：{0} （结果：亲，货主已完成订单运费支付。订单编号：ZFWLP1901022XXQXBB）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.DriverApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { driverEntity.DriverId };
        }
        #endregion

        //#region 用户收益 
        //private void MakeUserIncome(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        //{
        //    var inCome = this.userFlowRecordData.Get(orderDto.OrderNo, FlowDirectionType.Income, FlowType.Cod).GetAwaiter().GetResult();

        //    decimal codAmount = inCome.Amount;
        //    //亲，您有一笔代收货款已到账，金额：{0} 元，订单编号：{1} （结果：亲，您有一笔代收货款已到账，金额：1133 元，订单编号：ZFWLP1901022XXQXBB）
        //    string body = pushMessageConfigEntity.MessageTemplate;
        //    body = string.Format(body, codAmount, orderDto.OrderNo);

        //    pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
        //    pushMessageMongoEntity.AppType = AppType.ClientApp;
        //    pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
        //    pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
        //    pushMessageMongoEntity.Body = body;
        //    pushMessageMongoEntity.CreateTime = DateTime.Now;
        //    pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
        //    {
        //        OrderId = orderDto.OrderId,
        //        MessageType = PushMessageObjType.Order.ToString(),
        //        OrderType = pushMessageConfigEntity.PushMessageType.ToString()
        //    }, GeneralContainer.JsonSerializerSettings);
        //    pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        //}
        //#endregion

        //#region 司机收益 
        //private void MakeDriverIncome(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        //{
        //    var inCome = this.userFlowRecordData.Get(orderDto.OrderNo, FlowDirectionType.Income, FlowType.ShippingCost).GetAwaiter().GetResult();

        //    decimal shippingCostAmount = inCome.Amount;
        //    //亲，您有一笔订单运费已到账，金额：{0} 元，订单编号：{1} （结果：亲，您有一笔订单运费已到账，金额：1133 元，订单编号：ZFWLP1901022XXQXBB）
        //    string body = pushMessageConfigEntity.MessageTemplate;
        //    body = string.Format(body, shippingCostAmount, orderDto.OrderNo);

        //    pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
        //    pushMessageMongoEntity.AppType = AppType.DriverApp;
        //    pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
        //    pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
        //    pushMessageMongoEntity.Body = body;
        //    pushMessageMongoEntity.CreateTime = DateTime.Now;
        //    pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
        //    {
        //        OrderId = orderDto.OrderId,
        //        MessageType = PushMessageObjType.Order.ToString(),
        //        OrderType = pushMessageConfigEntity.PushMessageType.ToString()
        //    }, GeneralContainer.JsonSerializerSettings);
        //    pushMessageMongoEntity.UserIds = new long[] { inCome.UserId };
        //}
        //#endregion

        //#region 合伙人收益 
        //private void MakePartnerIncome(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        //{
        //    var inCome = this.userFlowRecordData.Get(orderDto.OrderNo, FlowDirectionType.Income, FlowType.OrderInfoFee).GetAwaiter().GetResult();

        //    decimal infoFeeAmount = inCome.Amount;
        //    //亲，您有一笔订单分佣已到账，金额：{0} 元 （结果：亲，您有一笔订单分佣已到账，金额：3 元）
        //    string body = pushMessageConfigEntity.MessageTemplate;
        //    body = string.Format(body, infoFeeAmount, orderDto.OrderNo);

        //    pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
        //    pushMessageMongoEntity.AppType = AppType.PartnerApp;
        //    pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
        //    pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
        //    pushMessageMongoEntity.Body = body;
        //    pushMessageMongoEntity.CreateTime = DateTime.Now;
        //    pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
        //    {
        //        OrderId = orderDto.OrderId,
        //        MessageType = PushMessageObjType.Order.ToString(),
        //        OrderType = pushMessageConfigEntity.PushMessageType.ToString()
        //    }, GeneralContainer.JsonSerializerSettings);
        //    pushMessageMongoEntity.UserIds = new long[] { inCome.UserId };
        //}
        //#endregion

        #region 用户退款 
        private void MakeUserRefund(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //TODO:退款推送还没完成
            //var inCome = this.userFlowRecordData.Get(orderDto.OrderNo, FlowDirectionType.Income, FlowType.Cod).GetAwaiter().GetResult();

            //decimal codAmount = inCome.Amount;
            ////亲，您的订单：{0} 退款申请已被确认通过，退款金额：{1} 元 （结果：亲，您的订单：ZFWLP1901022XXQXBB 退款申请已被确认通过，退款金额：1133 元）
            //string body = pushMessageConfigEntity.MessageTemplate;
            //body = string.Format(body, codAmount, orderDto.OrderNo);

            //pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            //pushMessageMongoEntity.AppType = AppType.ClientApp;
            //pushMessageMongoEntity.BarTypeId = GlobalVar.ANDROID_NOTIFICATION_BAR_TYPE;
            //pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            //pushMessageMongoEntity.Body = body;
            //pushMessageMongoEntity.CreateTime = DateTime.Now;
            //pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            //{
            //    OrderId = orderDto.OrderId,
            //    MessageType = PushMessageObjType.Order.ToString()
            //}, GeneralContainer.JsonSerializerSettings);
            //pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 司机退款 

        #endregion

        //#region 司机支付网络平台使用费 
        //private void MakeOrderInfoFee(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        //{
        //    var orderInfoFeeOutlay = this.userFlowRecordData.Get(orderDto.OrderNo, FlowDirectionType.Outlay, FlowType.OrderInfoFee).GetAwaiter().GetResult();

        //    decimal orderInfoFeeAmount = orderInfoFeeOutlay.Amount;
        //    //亲，您已成功支付订单网络平台使用费：{0} 元，订单编号：{1}，请尽快与货主取得联系。 （结果：亲，您已成功支付订单网络平台使用费：1133 元，订单编号：ZFWLP1901022XXQXBB，请尽快与货主取得联系。）
        //    string body = pushMessageConfigEntity.MessageTemplate;
        //    body = string.Format(body, orderInfoFeeAmount, orderDto.OrderNo);

        //    pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
        //    pushMessageMongoEntity.AppType = AppType.DriverApp;
        //    pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
        //    pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
        //    pushMessageMongoEntity.Body = body;
        //    pushMessageMongoEntity.CreateTime = DateTime.Now;
        //    pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
        //    {
        //        OrderId = orderDto.OrderId,
        //        MessageType = PushMessageObjType.Order.ToString(),
        //        OrderType = pushMessageConfigEntity.PushMessageType.ToString()
        //    }, GeneralContainer.JsonSerializerSettings);
        //    pushMessageMongoEntity.UserIds = new long[] { orderInfoFeeOutlay.UserId };
        //}
        //#endregion

        #region 司机收款完成 
        private void MakePOS(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            decimal codAmount = (orderDto.CodCostAmount.Value - orderDto.CodChargeAmount.Value);

            //亲，您有一笔订单已收款完成，订单编号：{0}，收款金额：{1} 元 （结果：亲，您有一笔订单已收款完成，订单编号：ZFWLP1901022XXQXBB，收款金额：1133 元）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, codAmount, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 司机取消订单审核未通过/通过
        private void MakeDriverCancelFailed(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，订单编号:{0} 取消订单审核未通过。  （结果：亲，订单编号:{0} 取消订单审核未通过。）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);
            var driverEntity = this.driverData.GetDriverByIdAsync(orderDto.DriverId.Value).GetAwaiter().GetResult();

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.DriverApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Validation.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { driverEntity.DriverId };
        }
        #endregion

        public PushMessageMongoEntity GetPushMessageMongoEntity(long orderId, PushMessageType pushMessageType, decimal distance, int? timeSpent, DateTime? preestimateArrivalTime, string endAddress)
        {
            PushMessageMongoEntity pushMessageMongoEntity = new PushMessageMongoEntity();

            PushMessageConfigEntity pushMessageConfigEntity = GetPushMessageConfigInfo(pushMessageType);

            OrderDto orderDto = this.orderData.GetEntity(orderId).GetAwaiter().GetResult().As<OrderDto>();

            switch (pushMessageType)
            {
                case PushMessageType.BoundVehicleDistince:
                    MakeBoundVehicleDistince(orderDto, pushMessageConfigEntity, pushMessageMongoEntity, distance, timeSpent, null, endAddress);
                    break;
                case PushMessageType.LoadCompletedSetOut:
                    MakeLoadCompletedSetOut(orderDto, pushMessageConfigEntity, pushMessageMongoEntity, null, preestimateArrivalTime, endAddress);
                    break;

            }
            return pushMessageMongoEntity;
        }

        #region 司机已接单距离通知
        private void MakeBoundVehicleDistince(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity, decimal distance, int? timeSpent, DateTime? preestimateArrivalTime, string endAddress)
        {
            //您发往{0}的订单，司机已接单，车辆距离您{1}公里，预计{2}小时到达。
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, endAddress, distance / 1000, timeSpent ?? 0);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 车辆装货完成出发
        private void MakeLoadCompletedSetOut(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity, int? timeSpent, DateTime? preestimateArrivalTime, string endAddress)
        {
            //您发往{0}的订单，已出发，预计到达时间{1}。
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, endAddress, preestimateArrivalTime);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion

        #region 订单完成推送用户
        private void MakeOrderComplete(OrderDto orderDto, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，您的订单 {0} 已完成  （结果：亲，您的订单 ZFWLP1901022XXQXBB 已完成）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderDto.OrderNo);

            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.ClientApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                OrderId = orderDto.OrderId,
                MessageType = PushMessageObjType.Order.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { orderDto.UserId };
        }
        #endregion
    }
}
